// Copyright 2011 Google Inc. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.


/**
 * @fileoverview Unit tests for shellExtension.status.
 *
 * Test cases:
 *   testDetermineLoadedStatus
 *   testDetermineInitStatus
 *   testDetermineFailedStatus
 *   testDetermineStatusBadInput
 *   testIsValidStatus
 *
 * @author jasonstredwick@google.com (Jason Stredwick)
 */


goog.require('shellExtension.status');


/**
 * Helper data for reducing code size within the test.
 * @type {shellExtension.Status}
 */
var FAILED = shellExtension.status.Value.FAILED;


/**
 * Helper data for reducing code size within the test.
 * @type {shellExtension.Status}
 */
var INIT = shellExtension.status.Value.INIT;


/**
 * Helper data for reducing code size within the test.
 * @type {shellExtension.Status}
 */
var LOADED = shellExtension.status.Value.LOADED;


/**
 * Asserts if the given value is not shellExtension.status.Value.FAILED.
 * @param {string} value The value to check.
 * @param {string=} opt_comment An optional comment to pass to the assert.
 */
function assertFailed(value, opt_comment) {
  assertEquals(comment, shellExtension.status.Value.FAILED, value);
}


/**
 * Asserts if the given value is not shellExtension.status.Value.INIT.
 * @param {string} value The value to check.
 * @param {string=} opt_comment An optional comment to pass to the assert.
 */
function assertInit(value, opt_comment) {
  assertEquals(comment, shellExtension.status.Value.INIT, value);
}


/**
 * Asserts if the given value is not shellExtension.status.Value.LOADED.
 * @param {string} value The value to check.
 * @param {string=} opt_comment An optional comment to pass to the assert.
 */
function assertLoaded(value, opt_comment) {
  assertEquals(comment, shellExtension.status.Value.LOADED, value);
}


/**
 * Tests determineStatus function's ability to handle arrays with LOADED
 * status.
 */
function testDetermineLoadedStatus() {
  var data = [LOADED];
  assertLoaded(shellExtension.status.determine(data));

  data = [LOADED, LOADED, LOADED];
  assertLoaded(shellExtension.status.determine(data));
}


/**
 * Tests determineStatus function's ability to handle arrays with INIT status.
 */
function testDetermineInitStatus() {
  var data = [INIT];
  assertInit(shellExtension.status.determine(data));

  // Test init vs loaded.
  data = [INIT, LOADED];
  assertInit(shellExtension.status.determine(data));

  // Test init last in list.
  data = [LOADED, LOADED, INIT];
  assertInit(shellExtension.status.determine(data));
}


/**
 * Tests determineStatus function's ability to handle arrays with a FAILED
 * status.
 */
function testDetermineFailedStatus() {
  // Test no data.
  var data = [];
  assertFailed(shellExtension.status.determine(data));

  // Test only failed.
  data = [FAILED];
  assertFailed(shellExtension.status.determine(data));

  data = [FAILED, FAILED];
  assertFailed(shellExtension.status.determine(data));

  // Test failed as first value.
  data = [FAILED, LOADED];
  assertFailed(shellExtension.status.determine(data));

  // Test failed as last value.
  data = [LOADED, LOADED, FAILED];
  assertFailed(shellExtension.status.determine(data));

  // Test failed as random value.
  data = [LOADED, LOADED, FAILED, LOADED];
  assertFailed(shellExtension.status.determine(data));

  // Test failed as multiple random values.
  data = [LOADED, LOADED, FAILED, LOADED, LOADED, FAILED, LOADED];
  assertFailed(shellExtension.status.determine(data));

  // Test failed versus init.
  data = [INIT, FAILED];
  assertFailed(shellExtension.status.determine(data));
}


/**
 * Tests determineStatus function's ability to handle bad input including
 * no input, non-array, and bad status values.
 */
function testDetermineStatusBadInput() {
  // Test no input.
  assertFailed(shellExtension.status.determine());

  // Test non-array input.
  assertFailed(shellExtension.status.determine(LOADED));

  // Test non-status input values.
  assertFailed(shellExtension.status.determine('other'));
}


/**
 * Tests whether or not given values are valid status values.  Looks at all
 * valid status values from the enumeration and some invalid values.
 */
function testIsValidStatus() {
  // Test enumeration values.
  for (var key in shellExtension.Status) {
    var value = shellExtension.Status[key];
    assertTrue(shellExtension.status.isValid(value);
  }

  assertFalse(shellExtension.status.isValid());
  assertFalse(shellExtension.status.isValid(null));
  assertFalse(shellExtension.status.isValid({}));
  assertFalse(shellExtension.status.isValid(''));
  assertFalse(shellExtension.status.isValid('not-status'));
}

